home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 424_02 / ED-157 / urename.c < prev    next >
C/C++ Source or Header  |  1993-12-18  |  1KB  |  39 lines

  1. /*
  2.  * Copyright (C) 1993 by Charles Sandmann (sandmann@clio.rice.edu)
  3.  * 
  4.  * This file is part of ED.
  5.  * 
  6.  * ED is free software; you can redistribute it and/or modify it under the terms
  7.  * of the GNU General Public License as published by the Free Software Foundation.
  8.  * 
  9.  * ED is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  10.  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  11.  * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
  12.  * 
  13.  * You should have received a copy of the GNU General Public License along with ED
  14.  * (see the file COPYING).  If not, write to the Free Software Foundation, 675
  15.  * Mass Ave, Cambridge, MA 02139, USA.
  16.  */
  17.  
  18. /* Provides unix-line rename; will clobber the destination if exists */
  19. #include <stdio.h>
  20. #include <sys\stat.h>
  21. #include <errno.h>
  22.  
  23. int unix_rename(char *from, char *to)
  24. {
  25.     int i;
  26.     struct stat statbuf;
  27.     
  28.     if(!(i = rename(from,to)))
  29.         return i;
  30. #ifdef WIN32
  31.     if(errno != EEXIST)            /* NT specific */
  32.         return i;
  33. #endif
  34.     if(!stat(from,&statbuf))    /* Check if from file exists */
  35.         if(!stat(to,&statbuf))    /* Check if to file exists */
  36.             remove(to);            /* Both do, delete to file */
  37.     return rename(from,to);        /* Retry rename */
  38. }
  39.